//
//  GalleryTopView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/31.
//

#import "GalleryTopView.h"
#import "CommonUtil.h"

@interface GalleryTopView()

@property (strong, nonatomic) UIScrollView * contentView;
@property (strong, nonatomic) UIView * line1;
@property (strong, nonatomic) UIButton * selectBtn;
@property (strong, nonatomic) UIView * indexView;

@end

@implementation GalleryTopView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {


    }
    return self;
}


-(void)setType:(DetailPageType)type{
    _type = type;
    self.titles = @[@"036期",@"035期",@"034期",@"033期",@"032期",@"031期",@"033期",@"032期",@"031期",@"033期"];
    
    if (self.type == DetailPageTypeGallery) {
        
    }else{
        self.contentView.width = self.width;
        self.timeBtn.hidden = YES;
    }
    
    [self addSubview:self.contentView];
    [self addSubview:self.timeBtn];
    
    for (NSInteger i = 0; i < self.titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(255.0, 107.0, 130.0) forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
        btn.backgroundColor = [UIColor whiteColor];
        btn.tag = i;
        [self.contentView addSubview:btn];
        btn.frame = CGRectMake(i * AD(60), 0, AD(60), self.height);
        if (i == 0) {
            [self btnClick:btn];
        }else if (i == self.titles.count - 1){
            self.contentView.contentSize = CGSizeMake(btn.right, 0);
        }
       
    }
    
    [self.contentView addSubview:self.indexView];
    [self addSubview:self.line1];
    
}

- (void)btnClick:(UIButton *)sender{
    self.selectBtn.selected = false;
    self.selectBtn = sender;
    self.selectBtn.selected = YES;
    self.indexView.centerX = sender.centerX;
    CGFloat cha = sender.centerX - (self.contentView.width/2);
    CGFloat cha2 = self.contentView.contentSize.width - (self.contentView.width);
    if (cha > 0 && cha < cha2){
        [self.contentView setContentOffset:CGPointMake(cha, 0) animated:YES];
    }
    if (self.actionBlock) {
        self.actionBlock(sender.tag);
    }
}




#pragma mark - lazy

-(UIScrollView *)contentView{
    if (!_contentView) {
        _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-AD(60), self.height)];
        _contentView.showsHorizontalScrollIndicator = false;
    }
    return _contentView;
}

-(UIView *)line1{
    if (!_line1) {
        _line1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-AD(1), SCREEN_WIDTH, AD(1))];
        _line1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    }
    return _line1;
}

-(UIView *)indexView{
    if (!_indexView) {
        _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-AD(5), AD(60), AD(1))];
        _indexView.backgroundColor = RGB(255.0, 107.0, 130.0);
     
    }
    return _indexView;
}
-(UIButton *)timeBtn{
    if (!_timeBtn) {
        _timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_timeBtn setTitle:@"2021" forState:UIControlStateNormal];
        [_timeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _timeBtn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
        _timeBtn.backgroundColor = [UIColor whiteColor];
        [_timeBtn setImage:[UIImage imageNamed:@"icon_gallery_down"] forState:UIControlStateNormal];
        _timeBtn.frame = CGRectMake(self.contentView.right, 0, AD(60), self.height);
        _timeBtn.imageEdgeInsets = UIEdgeInsetsMake(AD(5), AD(40), 0, 0);
        _timeBtn.titleEdgeInsets = UIEdgeInsetsMake(AD(5), 0, 0, AD(20));
        _timeBtn.layer.borderColor = [[UIColor blackColor] colorWithAlphaComponent:0.1].CGColor;
        _timeBtn.layer.borderWidth = AD(1);
    }
    return _timeBtn;
}

@end
